#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <string.h>

#include "uart.h"
#include "adc.h"
#include "spi.h"
#include "rf12.h"
#include "uart_command.h"
#include "tickcount.h"
#include "charger.h"

#define BAUD 115200





unsigned char *packet;
unsigned char packet_len;
uint16_t tc_test;
uint16_t tc_charger_measures;



int main( void )
{
	TickcountPrepare();
	uart_init((UART_BAUD_SELECT((BAUD),F_CPU)));
	prepare_adc_measure();
	Spi_Init();
	RF12_Init();
	RFM12B_SetTransmitMode();
	InitCharger();
	sei(); /* enable interrupt */


	tc_charger_measures=tc_test=GetTickCount();

	uart_puts_P( "[*Ready*]" );

	while(1)  /* main process loop */
	{

		/* receive */

		if(RFM12B_ReadPacket(&packet,&packet_len)==2)
		{
			uart_puts_P( "[r" );

			for(int i=0;i<packet_len;i++) uart_puthex_byte(packet[i]);
			uart_puts_P( "]" );
		}

		UART_Process();
		ChargerProcess();

		if(GetTickSince(tc_test)>10000)
		{
			tc_test=GetTickCount();
			uart_puts_P( "[+]" );	
		}


		if(GetTickSince(tc_charger_measures)>1000)
		{
			tc_charger_measures=GetTickCount();
			UART_ProcessCommand("[C]", 3);	
		}

	}
			
}







